package com.yidian.controller;


import com.yidian.common.constant.ChannelConstant;
import com.yidian.common.entity.Result;
import com.yidian.common.utils.RedisUtils;
import com.yidian.entity.Channel;
import com.yidian.entity.ChannelUsersKey;
import com.yidian.service.ChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/channel")
@Api(value = "/channel", tags = "频道接口")
public class ChannelController {

    @Autowired
    private ChannelService channelService;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 获取所有频道
     *
     * @return
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ApiOperation(value = "获取所有频道", notes = "获取所有频道", httpMethod = "GET", response = Result.class)
    public Result<List<Channel>> getAllChannel() {
        //获取频道列表
        List<Channel> allChannel = channelService.getAllChannel();
        return Result.ok(allChannel);
    }

    /**
     * 添加用户频道
     *
     * @param channelUsersKey
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加用户频道", notes = "添加用户频道", httpMethod = "POST", response = Result.class)
    public Result addUserChannel(@RequestBody ChannelUsersKey channelUsersKey) {
        if (channelService.selectChannelByUserId(channelUsersKey.getUserId()) == null) {
            channelService.addUserChannel(channelUsersKey);
            List<Channel> channelList = channelService.selectChannelByUserId(channelUsersKey.getUserId());
            redisUtils.set(ChannelConstant.CHANNEL_KEY + channelUsersKey.getUserId(), channelList);
        } else {
            channelService.addUserChannel(channelUsersKey);
            redisUtils.delete(ChannelConstant.CHANNEL_KEY + channelUsersKey.getUserId());
            List<Channel> channelList = channelService.selectChannelByUserId(channelUsersKey.getUserId());
            redisUtils.set(ChannelConstant.CHANNEL_KEY + channelUsersKey.getUserId(), channelList);
        }
        return Result.ok();
    }

    /**
     * 删除用户频道
     *
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除用户频道", notes = "删除用户频道", httpMethod = "DELETE", response = Result.class)
    public Result deleteUserChannel(@RequestBody ChannelUsersKey channelUsersKey) {
        channelService.deleteUserChannel(channelUsersKey);
        redisUtils.delete(ChannelConstant.CHANNEL_KEY + channelUsersKey.getUserId());
        List<Channel> channelList = channelService.selectChannelByUserId(channelUsersKey.getUserId());
        redisUtils.set(ChannelConstant.CHANNEL_KEY + channelUsersKey.getUserId(), channelList);
        return Result.ok();
    }
}
